/**
 * 
 */
package de.midnightdance.model.user;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import de.cbf.util.uuidheader.entities.UUIDHeader;

/**
 * @author Surfer
 *
 */
public class RequiemUser extends UUIDHeader {
	public static final String PERMISSION_READ_ALL 	= "readAll";
	public static final String PERMISSION_WRITE_ALL = "writeAll";
	public static final String PERMISSION_ADMIN		= "admin";
		
	protected String name;
	protected String pass;
	protected List<String> permissions;
	
	
	
	public RequiemUser() {
		super();
		permissions = new ArrayList<String>();
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "RequiemUser (name=" + name + ", pass=" + pass
				+ ", permissions=" + permissions + ")";
	}

	@Override
	protected void takeOverEntityData(Object aOther) {
		if (aOther instanceof RequiemUser) {
			RequiemUser otherUser = (RequiemUser) aOther;
			this.name = otherUser.name;
			this.pass = otherUser.pass;
			this.permissions = new ArrayList<String>(otherUser.permissions);			
		}		
	}
	
	public String[] getPermissions() {
		return permissions.toArray(new String[permissions.size()]);
	}	
	public void setKinder(String[] value) {
		permissions.clear();
		permissions.addAll(Arrays.asList(value));
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPass() {
		return pass;
	}
	public void setPass(String pass) {
		this.pass = pass;
	}
	
	protected Iterator<String> getPermissionsIterator(){
		return permissions.iterator();
	}
	
	public boolean isPermitted(String permissionToCheck){		
		String first4Letters = permissionToCheck.substring(0, 4);
		if("read".equals(first4Letters)&&permissions.contains(PERMISSION_READ_ALL)){
			return true;
		}
		if("writ".equals(first4Letters)&&permissions.contains(PERMISSION_WRITE_ALL)){
			return true;
		}
		return permissions.contains(permissionToCheck);
	}
	
	public boolean addPermission(String permission){
		return permissions.add(permission);
	}
	
	public boolean removePermission(String permission){
		return permissions.remove(permission);
	}



	
	
}
